﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWebUtils;
using TTQT;

public partial class QuanTri_QuanTriTin_QuanLyTin : MasterControl//System.Web.UI.UserControl
{
    TinTucDAO tinTucDao = new TinTucDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bindCategory();
            bindGrid("-1", "-1");
            visiblePane(true,false);
            resetForm();
        }
    }
    private void bindGrid(string categoryId, string isActive)
    {
        try
        {
            detailGrid.DataSource = tinTucDao.tinTuc_GetByNhomAndIsActive(categoryId, isActive);
            detailGrid.DataBind();
        }
        catch (Exception)
        {
            detailGrid.CurrentPageIndex = 0;
            detailGrid.DataSource = tinTucDao.tinTuc_GetByNhomAndIsActive(categoryId, isActive);
            detailGrid.DataBind();
        }

    }
    private void bindCategory()
    {
        cboNhomTin.DataSource = tinTucDao.tinTuc_Nhom_GetAllForAdmin();
        cboNhomTin.DataBind();
        cboNhomTin0.DataSource = tinTucDao.tinTuc_Nhom_GetAllForAdmin();
        cboNhomTin0.DataBind();
    }
    protected void cmdSearch_Click(object sender, EventArgs e)
    {

    }
    protected void cmdUpload_Click(object sender, EventArgs e)
    {
        if (!anh.HasFile)
        {
            MyWebUtil.alert(this.Page, "Bạn chưa chọn file");
            return;
        }
        string thuMuc = Server.MapPath("../img/image/TinTuc");
        string fileName = anh.FileName;
        if (fileName.ToLower().EndsWith(".jpg") || fileName.ToLower().EndsWith(".bmp") || fileName.ToLower().EndsWith(".gif") || fileName.ToLower().EndsWith(".png") || fileName.ToLower().EndsWith(".jpeg"))
        {
            //if (File.Exists(thuMuc + "/" + fileName))
            //{
            //    MyWebUtil.alert(this.Page, "File này đã tồn tại. Bạn vui lòng chọn file khác hoặc đổi tên file!");
            //    return;
            //}
            anh.SaveAs(thuMuc + "/" + fileName);
            anh.Dispose();

            //sinh anh thumb
            string tenAnhThumb = "";
            string[] temp = fileName.Split('.');
            tenAnhThumb = temp[0] + "_s." + temp[1];
            XuLyAnh.createThumbImage(thuMuc + "/" + fileName, thuMuc + "/" + tenAnhThumb, 500);


            lblPathAnh.Text = "../img/image/TinTuc/" + tenAnhThumb;
            imgUpload.ImageUrl = "../" + lblPathAnh.Text;
            imgUpload.Visible = true;
            cmdXoaAnh.Visible = true;
        }
        else
        {
            MyWebUtil.alert(this.Page, "Chỉ chấp nhận các ảnh có định dạng jpg,bmp,gif,png");
            return;
        }
    }
    protected void detailGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        detailGrid.CurrentPageIndex = e.NewPageIndex;
        bindGrid(cboNhomTin.SelectedValue, cboActive.SelectedValue);
    }
    protected void cboNhomTin_SelectedIndexChanged(object sender, EventArgs e)
    {
        cboNhomTin0.SelectedValue = cboNhomTin.SelectedValue;
        bindGrid(cboNhomTin.SelectedValue, cboActive.SelectedValue);
    }
    protected void cboActive_SelectedIndexChanged(object sender, EventArgs e)
    {
        cboActive0.SelectedValue = cboActive.SelectedValue;
        bindGrid(cboNhomTin.SelectedValue, cboActive.SelectedValue);
    }
    protected void detailGrid_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName.Equals("cmdEdit"))//edit news
        {
            visiblePane(false, true);
            tinTucDao.NewsId = e.Item.Cells[0].Text;
            tinTucDao.getById();
            lblId.Text = tinTucDao.NewsId;
            try
            {
                cboNhomTin0.SelectedValue = tinTucDao.CategoryId;
            }
            catch (Exception ex) { }
            try
            {
                cboActive0.SelectedValue = tinTucDao.IsActive;
            }
            catch (Exception ex) { }
            
            chkHotSite.Checked = tinTucDao.Hotsite.Equals("1");
            lblPathAnh.Text = tinTucDao.ImagePath;
            imgUpload.ImageUrl = lblPathAnh.Text;
            txtTomLuoc.Text = tinTucDao.Summary;
            txtTitle.Text = tinTucDao.Title;
            fck.Value = tinTucDao.Content;
            txtSeoTitle.Text = tinTucDao.SeoTitle;
            txtSeoKeywords.Text = tinTucDao.SeoKeyword;
            txtSeoDescription.Text = tinTucDao.SeoDescription;
            if (!string.IsNullOrEmpty(tinTucDao.ImagePath))
            {
                imgUpload.ImageUrl = "../" + tinTucDao.ImagePath;
                lblPathAnh.Text = tinTucDao.ImagePath;
                imgUpload.Visible = true;
                //lblPathAnh.Visible = true;
                cmdXoaAnh.Visible = true;
            }

        }
        if (e.CommandName.Equals("cmdDelete"))//edit news
        {
            visiblePane(false, true);
            tinTucDao.NewsId = e.Item.Cells[0].Text;
            if(tinTucDao.delete()>0)
            {
                resetForm();
                visiblePane(true,false);
                bindGrid(cboNhomTin.SelectedValue, cboActive.SelectedValue);
            }

        }
    }
    private bool kiemLoiNhap()
    {
        bool kq = true;
        if (cboNhomTin0.SelectedValue.Equals("-1"))
        {
            MyWebUtils.MyWebUtil.alert(this.Page, "Bạn chưa chọn mục tin");
            return false;
        }
        if (cboActive0.SelectedValue.Equals("-1"))
        {
            MyWebUtils.MyWebUtil.alert(this.Page, "Bạn chưa chọn trạng thái");
            return false;
        }
        if (string.IsNullOrEmpty(txtTitle.Text.Trim()))
        {
            MyWebUtils.MyWebUtil.alert(this.Page, "Bạn chưa nhập tiêu đề");
            return false;
        }
        return kq;
    }
    private void visiblePane(bool grid, bool edit)
    {
        pnGridDetail.Visible = grid;
        pnEditDetail.Visible = edit;
    }
    private void resetForm()
    {
        lblId.Text = string.Empty;
        lblPathAnh.Text = string.Empty;
        imgUpload.ImageUrl = string.Empty;
        cboNhomTin0.SelectedValue = cboNhomTin.SelectedValue;
        cboActive0.SelectedValue = cboActive.SelectedValue;
        txtTitle.Text = string.Empty;
        txtTomLuoc.Text = string.Empty;
        fck.Value = string.Empty;
    }
    protected void cmdCapNhatDetail_Click(object sender, EventArgs e)
    {
        
        if (string.IsNullOrEmpty(lblId.Text))//them moi
        {
            if (!kiemLoiNhap())
            {
                return;
            }
            tinTucDao = new TinTucDAO();
            tinTucDao.CategoryId = cboNhomTin0.SelectedValue;
            tinTucDao.Title = txtTitle.Text;

            tinTucDao.SeoTitle = txtSeoTitle.Text;
            tinTucDao.SeoKeyword = txtSeoKeywords.Text;
            tinTucDao.SeoDescription = txtSeoDescription.Text;

            tinTucDao.IsActive = cboActive0.SelectedValue;
            tinTucDao.Hotsite = chkHotSite.Checked ? "1" : "0";
            tinTucDao.ImagePath = lblPathAnh.Text;
            tinTucDao.Summary = txtTomLuoc.Text;
            tinTucDao.Content = fck.Value;
            tinTucDao.UserId = UserInfo.userName;
            tinTucDao.NewsId = tinTucDao.insert().ToString();

            if (TTQT.TTQT_Code.TTQTCommon.ToInt32(tinTucDao.NewsId, 0) > 0)
            {
                visiblePane(true, false);
                resetForm();
                bindGrid(cboNhomTin.SelectedValue, cboActive.SelectedValue);
            }
        }
        else//update
        {
            if (!kiemLoiNhap())
            {
                return;
            }
            tinTucDao = new TinTucDAO();
            tinTucDao.NewsId = lblId.Text;
            tinTucDao.CategoryId = cboNhomTin0.SelectedValue;
            tinTucDao.IsActive = cboActive0.SelectedValue;
            tinTucDao.Title = txtTitle.Text;

            tinTucDao.SeoTitle = txtSeoTitle.Text;
            tinTucDao.SeoKeyword = txtSeoKeywords.Text;
            tinTucDao.SeoDescription = txtSeoDescription.Text;

            tinTucDao.Hotsite = chkHotSite.Checked ? "1" : "0";
            tinTucDao.ImagePath = lblPathAnh.Text;
            tinTucDao.Summary = txtTomLuoc.Text;
            tinTucDao.Content = fck.Value;
            tinTucDao.UserId = UserInfo.userName;
            //tinTucDao.NewsId = tinTucDao.insert().ToString(CultureInfo.InvariantCulture);
            if (tinTucDao.update() > 0)
            {
                visiblePane(true, false);
                resetForm();
                bindGrid(cboNhomTin.SelectedValue, cboActive.SelectedValue);
            }
        }
    }
    protected void cmdAddDetail_Click(object sender, EventArgs e)
    {
        resetForm();
        visiblePane(false,true);
    }
    protected void cmdHuyBoDetail_Click(object sender, EventArgs e)
    {
        resetForm();
        visiblePane(true, false);
    }
    protected void cmdXoaAnh_Click(object sender, EventArgs e)
    {
        try
        {
            File.Delete(Server.MapPath(lblPathAnh.Text));
            try
            {
                File.Delete(Server.MapPath(lblPathAnh.Text.Replace("_s", "")));
            }
            catch { }
            lblPathAnh.Text = "";
            imgUpload.ImageUrl = "";
            cmdXoaAnh.Visible = false;
        }
        catch (Exception ex)
        {
        }
    }
}
